﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LearningBubbles
{
    class Assignment
    {
        string text;
        InteractionType iType;

        internal InteractionType IType
        {
            get { return iType; }
            set { iType = value; }
        }
        AssignmentType assType;

        internal AssignmentType AssType
        {
            get { return assType; }
            set { assType = value; }
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        private List<List<Element>> elements;

        public List<List<Element>> Elements
        {
            get { return elements; }
            set { elements = value; }
        }

        private List<string> elementStrings;

        public List<string> ElementStrings
        {
            get { return elementStrings; }
            set { elementStrings = value; }
        }

        public Assignment(int assId, InteractionType iType, AssignmentType assType)
        {
            this.assType = assType;
            this.iType = iType;
            text = GetText(assId);
            elements = GetElements(assId);
            SetElementPositions();
            elementStrings = InsertStrings(elements);
            //dict = insertElementsToDict(text,elements);


        }

        public List<string> InsertStrings(List<List<Element>> l)
        {
            List<string> elementseee = new List<string>();
            foreach(List<Element> list in l)
            {
                elementseee.Add(list.FirstOrDefault().Value.ToString());
            }
            return elementseee;
        }

        public List<List<Element>> GetElements(int assId)
        {
            Dictionary<element, List<bogus>> dictionary = new Dictionary<element, List<bogus>>();
            List<List<Element>> dictionary1 = new List<List<Element>>();
            List<element> elementss = Queries.GetElements(assId);
            Random random = new Random();
          
            switch (assType)
            {
                case AssignmentType.QUIZ:
                    foreach (element e in elementss)
                    {
                        dictionary.Add(e, Queries.GetBogus(e.id));
                    }
                    dictionary1 = GetWords(dictionary);
                    break;
                case AssignmentType.DICTATION:
                    break;
            }
            return dictionary1;
        }

        /// <summary>
        /// Returns a random letter from A-z
        /// </summary>
        /// <param name="random"></param>
        /// <returns></returns>
        public char GetRandomLetter(Random random)
        {
            char c = (char)random.Next(65, 122);
            return c;
        }

        public List<List<Element>> GetWords(Dictionary<element, List<bogus>> dictionary)
        {
            List<List<Element>> dict1 = new List<List<Element>>();
            foreach (element e in dictionary.Keys)
            {
                List<Element> list1 = new List<Element>();
                list1.Add(new Word(e.element1));
                foreach (bogus bogus in dictionary[e])
                {
                    list1.Add(new Word(bogus.element));
                }
                dict1.Add(list1);
            }
            return dict1;
        }

        public string GetText(int id)
        {
            return Queries.GetAssignment(id).text;
        }

        public void SetElementPositions()
        {
            int pos = 0;
            foreach (List<Element> e in elements)
            {
                while (e.First().PosInText == 0 && pos <= text.Length)
                {
                    while (text.ElementAt(pos) != '?' && text.ElementAt(pos+1) != '?' && text.ElementAt(pos+2) != '?')
                    {
                        pos++;
                    }
                    e.First().PosInText = pos;
                    if (pos + 2 < text.Length) pos += 2; else break;
                }
                
            }
        }

        //public Dictionary<int, Dictionary<Element, List<Element>>> insertElementsToDict(string text, Dictionary<Element, List<Element>> elements)
        //{
        //    Dictionary<int, Dictionary<Element, List<Element>>> dict = new Dictionary<int, Dictionary<Element, List<Element>>>();
        //    for (int i = 0; i < text.Length; i++)
        //    {
        //        if (text.ElementAt(i) == '%' && text.ElementAt(i + 1) == 'y')
        //        {
                    
        //            dict.Add(i, elements.FirstOrDefault());
        //            elements.Remove(elements.FirstOrDefault());
        //        }
        //    }
        //    return dict;
        //}
    }
}
